/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 *
 * @author caos
 */
public class ServerReader extends Thread {
    private ConcurrentLinkedQueue<String> inQueue;
    private BufferedReader bR;
    ServerReader(BufferedReader bR)
    {
        inQueue=new ConcurrentLinkedQueue();
        this.bR=bR;
    }
    
    String pollQueue()
    {
        if(inQueue.isEmpty())
            return null;
        return inQueue.poll();        
    }

    @Override
    public void run()
    {
        while(true)
        {
            try
            {
                StringBuilder b=new StringBuilder();
                char t=0;
                while(bR.ready())
                {
                    t=(char)bR.read();
      
                    if(t==(char)4)
                        break;
                    b.append(t);
                }
            
                if(!b.toString().trim().isEmpty())      
                {
                    String message=b.toString().trim();
                    System.out.println(">>> " + message);
                    inQueue.add(message);
                }
                sleep(200);
             } 
            catch (InterruptedException ex) {
                return;
            }            
            catch (IOException ex) 
            {
                System.err.append("Error 44:ServerReader " + ex.getMessage());
            }            
        }
    }
}
